from flask import jsonify, request
from . import api_bp, load_json_data
from config import Config

@api_bp.route('/regions', methods=['GET'])
def get_regions():
    data = load_json_data(Config.JSON_FILES['regions'])
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 10, type=int)
    start = (page - 1) * per_page
    end = start + per_page
    return jsonify(data[start:end])

@api_bp.route('/regions/<int:region_id>', methods=['GET'])
def get_region(region_id):
    data = load_json_data(Config.JSON_FILES['regions'])
    region = next((item for item in data if item['id'] == region_id), None)
    if region:
        return jsonify(region)
    return jsonify({"error": "Region not found"}), 404